/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */ 
package who.tmap.ui.dataimportwiz;

import java.awt.AWTEvent;
import java.awt.Frame;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.Map;

import javax.swing.*;

import who.tmap.bo.Attrs;
import who.tmap.common.TMapException;
import who.tmap.services.ODBCDSNLessService;
import who.tmap.ui.util.FileChooserFilter;
import who.tmap.ui.util.MiscUIHelper;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JDialogDataImportWizard extends JDialog {
	
	private static final int NB_PANELS =4;
	
	private static final int INTRO_PANEL =0;
	private static final int DS_SELECTION_PANEL =1;
	private static final int IMPORT_PANEL =2;
	private static final int FINISH_PANEL =3;
	
	private Attrs attrs =null;

	private javax.swing.JPanel jContentPane = null;

	private javax.swing.JSplitPane jSplitPane = null;
	private javax.swing.JPanel jPanelInfo = null;
	private javax.swing.JPanel jPanelNav = null;
	private javax.swing.JButton jButtonPrev = null;
	private javax.swing.JButton jButtonNext = null;
	private javax.swing.JButton jButtonCancel = null;
	private javax.swing.JButton jButtonFinish = null;
	private javax.swing.JPanel jPanel = null;
	private javax.swing.JLabel jLabel = null;	
		
	private javax.swing.JDesktopPane jDesktopPane = null;
	
	private JFrameIntroDataImportWizard jFrameIntroWizard =new JFrameIntroDataImportWizard();	
	private JFrameCSVImportWizard jFrameCSVImportWizard =new JFrameCSVImportWizard();
	private JFrameExcelImportWizard jFrameExcelImportWizard =new JFrameExcelImportWizard();
	private JFrameJDBCODBCImportWizard jFrameJDBCODBCImportWizard =new JFrameJDBCODBCImportWizard();
	
	private JFrameDSDataImportWizard jFrameDSDataImportWizard =new JFrameDSDataImportWizard();
	private JFrameFinishWizard jFrameFinishWizard =new JFrameFinishWizard();
	
	private int panelCt =INTRO_PANEL;
	private boolean cancelled =true;
	private boolean invokedByTMapWizard =false;
	private String adapterType;
	private File srcFile;
	
	/**
	 * This is the default constructor
	 */
	public JDialogDataImportWizard(Frame parent, boolean modal, String adapterType, File srcFile ) {	
		
		super(parent, modal);
		
		this.adapterType =adapterType;
		this.srcFile =srcFile;

		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		try {
		  initialize();
		}
		catch(Exception e) {
		  e.printStackTrace();
		}
	
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {

		this.setSize(600, 684);
		this.setContentPane(getJContentPane());
		this.setTitle("Data Import Wizard");
		
		displayPanelInfo();
		
		this.setLocationRelativeTo( null );
		
		jFrameFinishWizard.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() { 
			public void internalFrameActivated(javax.swing.event.InternalFrameEvent e) {    
			    jFrameFinishWizard.setInvokedByTMapWizard( invokedByTMapWizard );
			}
		});	
		
		// If the import wizard has been triggered 
		// by "open data" or "reload from ds"
		// select automatically the correct adapter
		// and move to the import panel
		if ( adapterType != null ) {

			if ( adapterType.equals( FileChooserFilter.EXCEL_EXT ) ) {
				jFrameDSDataImportWizard.getJComboBoxAdapter().setSelectedItem( JFrameDSDataImportWizard.EXCEL_ADAPTER );
				jFrameExcelImportWizard.getJButtonFileName().setVisible( false );
			} else if ( adapterType.equals( FileChooserFilter.CSV_EXT ) ) {
				jFrameDSDataImportWizard.getJComboBoxAdapter().setSelectedItem( JFrameDSDataImportWizard.CSV_ADAPTER );
				jFrameCSVImportWizard.getJButtonFileName().setVisible( false );
			} else if ( adapterType.equals( FileChooserFilter.MDB_EXT ) ) {
				jFrameDSDataImportWizard.getJComboBoxAdapter().setSelectedItem( JFrameDSDataImportWizard.JDBC_ODBC_ADAPTER );
				jFrameJDBCODBCImportWizard.getJButtonDatabase().setVisible( false );
			}
			
			panelCt =IMPORT_PANEL;
			displayPanelInfo();
			
			jButtonPrev.setVisible( false );
			jButtonNext.setVisible( false );
			jButtonFinish.setVisible( true );
			jFrameFinishWizard.setUpdateButtonClicked( true );
			
			cancelled =false;
		}
	}
	
	public void populateDsProperties( Map<String, String> dsProperties ) throws Exception {
		if ( adapterType.equals( FileChooserFilter.EXCEL_EXT ) ) {
			jFrameExcelImportWizard.populateDsProperties( dsProperties );
		} else if ( adapterType.equals( FileChooserFilter.CSV_EXT ) ) {
			jFrameCSVImportWizard.populateDsProperties( dsProperties );
		} else if ( adapterType.equals( FileChooserFilter.MDB_EXT ) ) {
			jFrameJDBCODBCImportWizard.populateDsProperties( dsProperties );
		}
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new java.awt.BorderLayout());
			jContentPane.add(getJPanel(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private javax.swing.JSplitPane getJSplitPane() {
		if(jSplitPane == null) {
			jSplitPane = new javax.swing.JSplitPane();
			jSplitPane.setTopComponent(getJPanelInfo());
			jSplitPane.setBottomComponent(getJPanelNav());
			jSplitPane.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
			jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
			jSplitPane.setDividerSize(0);
			jSplitPane.setResizeWeight(1);
			
		}
		return jSplitPane;
	}
	
	/**
	 * This method initializes jPanelInfo
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanelInfo() {
		if(jPanelInfo == null) {
			jPanelInfo = new javax.swing.JPanel();
			jPanelInfo.setLayout(new javax.swing.BoxLayout(jPanelInfo, javax.swing.BoxLayout.Y_AXIS));
			jPanelInfo.add(getJDesktopPane(), null);
			
		}
		return jPanelInfo;
	}
	
	/**
	 * This method initializes jPanelNav
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanelNav() {
		if(jPanelNav == null) {
			jPanelNav = new javax.swing.JPanel();
			jPanelNav.add(getJButtonPrev(), null);
			jPanelNav.add(getJButtonNext(), null);
			jPanelNav.add(getJButtonCancel(), null);
			jPanelNav.add(getJButtonFinish(), null);
		}
		return jPanelNav;
	}
	
	/**
	 * This method initializes jButtonPrev
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getJButtonPrev() {
		if(jButtonPrev == null) {
			jButtonPrev = new javax.swing.JButton();
			jButtonPrev.setText("<< Back");
			jButtonPrev.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ( panelCt > 0 ) {
						--panelCt;
						displayPanelInfo();
					}    
				}
			});
		}
		return jButtonPrev;
	}
	
	/**
	 * This method initializes jButtonNext
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getJButtonNext() {
		if(jButtonNext == null) {
			jButtonNext = new javax.swing.JButton();
			jButtonNext.setText("Next >>");
			jButtonNext.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ( panelCt < NB_PANELS ) {    
						panelCt++;
						displayPanelInfo();
					}
				}
			});
		}
		return jButtonNext;
	}
	
	/**
	 * This method initializes jButtonCancel
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getJButtonCancel() {
		if(jButtonCancel == null) {
			jButtonCancel = new javax.swing.JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					cancelled =true;
					attrs =null;
					setVisible( false );
				}
			});
		}
		
		return jButtonCancel;
	}
	
	/**
	 * This method initializes jButtonFinish
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getJButtonFinish() {
		if(jButtonFinish == null) {
			jButtonFinish = new javax.swing.JButton();
			jButtonFinish.setText("Finish");
			jButtonFinish.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					cancelled =false;
					setVisible( false );
				}
			});
		}
		return jButtonFinish;
	}
		
	/**
	 * This method initializes jDesktopPane
	 * 
	 * @return javax.swing.JDesktopPane
	 */
	private javax.swing.JDesktopPane getJDesktopPane() {
		if(jDesktopPane == null) {
			jDesktopPane = new javax.swing.JDesktopPane();

			jDesktopPane.add( jFrameIntroWizard );
			jDesktopPane.add( jFrameDSDataImportWizard );
			jDesktopPane.add( jFrameCSVImportWizard );
			jDesktopPane.add( jFrameExcelImportWizard );
			jDesktopPane.add( jFrameJDBCODBCImportWizard );			
			jDesktopPane.add( jFrameFinishWizard );
		}
		return jDesktopPane;
	}
	
	/**Close the dialog*/
	void cancel() {
	  dispose();
	}
	
	private void displayPanelInfo() {	
	    String selectedItem;
	    
		jButtonPrev.setVisible( panelCt != 0 );
		jButtonNext.setVisible( panelCt != NB_PANELS - 1 );
		jButtonFinish.setVisible( panelCt == NB_PANELS - 1 );
		
		JInternalFrame frame =null;		
		
		switch( panelCt ) {
			case INTRO_PANEL:
				frame =jFrameIntroWizard;
				jLabel.setText( panelCt + 1 + "- Data Import Wizard Introduction");
				break;			
			
			case DS_SELECTION_PANEL:
				frame =jFrameDSDataImportWizard;
				jLabel.setText( panelCt + 1 + "- Please select the format of your data source");
				break;
				
			case IMPORT_PANEL:
			    selectedItem =jFrameDSDataImportWizard.getJComboBoxAdapter().getSelectedItem().toString();
			    
			    if ( JFrameDSDataImportWizard.CSV_ADAPTER.equals( selectedItem ) ) {
			        frame =jFrameCSVImportWizard;
			        if ( srcFile != null ) {
			        	jFrameCSVImportWizard.getJTextFieldFileName().setText( srcFile.getAbsolutePath() );
			        	try {
							jFrameCSVImportWizard.populateSampleData();
						} catch (TMapException e) {
							new MiscUIHelper().displayErrorMessage(this, e);
						}
			        }
			    } else if ( JFrameDSDataImportWizard.EXCEL_ADAPTER.equals( selectedItem ) ) {
			        frame =jFrameExcelImportWizard;
			        if ( srcFile != null ) {
				        jFrameExcelImportWizard.getJLabelFile().setText( srcFile.getAbsolutePath() );
				        try {
				        	jFrameExcelImportWizard.populateWorkSheets();
				        	jFrameExcelImportWizard.populateSampleData();
						} catch (TMapException e) {
							new MiscUIHelper().displayErrorMessage(this, e);
						}
			        }
			    } else if ( JFrameDSDataImportWizard.JDBC_ODBC_ADAPTER.equals( selectedItem ) ) {
			        frame =jFrameJDBCODBCImportWizard;
			        if ( srcFile != null ) {
				        jFrameJDBCODBCImportWizard.getJTextFieldDatabase().setText( srcFile.getAbsolutePath() );
				        jFrameJDBCODBCImportWizard.getJComboBoxODBCDriver().setSelectedItem( ODBCDSNLessService.ODBC_ACCESS_NAME );
				        try {
							jFrameJDBCODBCImportWizard.populateTables();
							jFrameJDBCODBCImportWizard.populateSampleData();
						} catch (TMapException e) {
							new MiscUIHelper().displayErrorMessage(this, e);
						}
			        }
			    } else {
			        panelCt =DS_SELECTION_PANEL;
			    }
				
			    if ( srcFile == null ) {
			    	jLabel.setText( panelCt + 1 + "- " );
			    } else {
			    	jLabel.setText( "" );
			    }
			    
			    jLabel.setText( jLabel.getText() + "Please specify import parameters" );
				
				break;
							
			case FINISH_PANEL:
			    jFrameFinishWizard.setInvokedByTMapWizard( invokedByTMapWizard );
			    
			    selectedItem =jFrameDSDataImportWizard.getJComboBoxAdapter()
			    									  .getSelectedItem().toString();

				jLabel.setText( panelCt + 1 + "- Final step: " );
				if ( invokedByTMapWizard ) {
				    jLabel.setText( jLabel.getText() + "Update data" );
				} else {
				    jLabel.setText( jLabel.getText() + "Save data" );				    
				}
			    
				try {
					attrs =retrieveAttrs();
				    
				    jFrameFinishWizard.setTable( attrs );
				} catch( TMapException e) {
				    e.printStackTrace();
				}
			    
				frame =jFrameFinishWizard;		
				
				break;				
		}
		
		if ( frame != null ) {

			try {
				frame.setVisible( true );
				frame.setSelected( true );
				frame.setMaximum( true );
			} catch (PropertyVetoException e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel() {
		if(jPanel == null) {
			jPanel = new javax.swing.JPanel();
			jPanel.add(getJLabel(), null);
		}
		return jPanel;
	}
	
	/**
	 * This method initializes jLabel
	 * 
	 * @return javax.swing.JLabel
	 */
	private javax.swing.JLabel getJLabel() {
		if(jLabel == null) {
			jLabel = new javax.swing.JLabel();
			jLabel.setText("This is the wizard comment");
		}
		return jLabel;
	}
	
	/**
	 * @return
	 */
	public boolean isCancelled() {
		return cancelled || !jFrameFinishWizard.isUpdateButtonClicked() ;
	}

    /**
     * @param invokedByTMapWizard The invokedByTMapWizard to set.
     */
    public void setInvokedByTMapWizard(boolean invokedByTMapWizard) {
        this.invokedByTMapWizard = invokedByTMapWizard;
    }
    
    public Attrs retrieveAttrs() throws TMapException {
	    String selectedItem =jFrameDSDataImportWizard.getJComboBoxAdapter()
		  .getSelectedItem().toString();
	    
	    Attrs attrs =null;
	    
	    if ( JFrameDSDataImportWizard.CSV_ADAPTER.equals( selectedItem ) ) {
	        attrs =jFrameCSVImportWizard.getValuesTable();
	    } else if ( JFrameDSDataImportWizard.EXCEL_ADAPTER.equals( selectedItem ) ) {
	        attrs =jFrameExcelImportWizard.getValuesTable();
	    } else if ( JFrameDSDataImportWizard.JDBC_ODBC_ADAPTER.equals( selectedItem ) ) {
	        attrs =jFrameJDBCODBCImportWizard.getValuesTable();
	    }	    
	    
	    return attrs;
    }
    
    /**
     * @return Returns the table.
     */
    public Attrs getAttrs() {
        return attrs;
    }
    
}  //  @jve:visual-info  decl-index=0 visual-constraint="10,10"
